package com.dengqi.mymodule.activity.loader.contacts;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dengqi.mymodule.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by dell on 2019/5/7
 */
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.AlbumViewHolder> {

    private List<Map<String, String>> datas = new ArrayList<>();

    public ContactsAdapter(ContentResolver contentResolver, Cursor cursor) {
        initData(contentResolver, cursor);
    }

    private void initData(ContentResolver contentResolver, Cursor mCursor) {
        if (mCursor != null && mCursor.moveToFirst()) {
            do {
                Map<String, String> maps = new LinkedHashMap<>();
                String id = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
                String name = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                //指定获取NUMBER这一列数据
                String number = ",";
                String[] phoneProjection = new String[]{
                        ContactsContract.CommonDataKinds.Phone.NUMBER
                };
                Cursor cursor = contentResolver.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        phoneProjection,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id,
                        null,
                        null
                );
                if (cursor != null && cursor.moveToFirst()) {
                    do {
                        number = number + "," + cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    } while (cursor.moveToNext());
                }
                maps.put("id", id);
                maps.put("name", name);
                maps.put("number", number.replace(",,", ""));
                datas.add(maps);
            } while (mCursor.moveToNext());
        }
    }

    @Override
    public AlbumViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album, null);
        return new AlbumViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AlbumViewHolder holder, int position) {
        String id = datas.get(position).get("id");
        String name = datas.get(position).get("name");
        String number = datas.get(position).get("number");

        holder.tvAlbumName.setText(id + " - " + name);
        holder.tvAlbumAmount.setText(number);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class AlbumViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivAlbum;
        private TextView tvAlbumName;
        private TextView tvAlbumAmount;

        public AlbumViewHolder(View itemView) {
            super(itemView);
            ivAlbum = (ImageView) itemView.findViewById(R.id.album_iv_album);
            tvAlbumName = (TextView) itemView.findViewById(R.id.album_tv_album_name);
            tvAlbumAmount = (TextView) itemView.findViewById(R.id.album_tv_amount);
        }
    }
}